//
//  TLS_LeftVC.m
//  Travel_World
//
//  Created by lanou on 15/10/12.
//  Copyright © 2015年 TLS. All rights reserved.
//
#import "TLS_LeftVC.h"
#import "TLSSingleton.h"
#import "TLST_saveListVC.h"

@interface TLS_LeftVC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) UITableView *setTableView;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, copy) NSString *cashPath;
@end

@implementation TLS_LeftVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    
    [self.placeLabel removeFromSuperview];
    self.titleArr = @[@"夜间模式",@"清除缓存",@"我的收藏"];
}
// 创建tableView
- (void)createTableView
{
    self.setTableView = [[UITableView alloc] initWithFrame:CGRectMake(150, (HEIGHT-60*3)/2, WIDTH-150, 60*3) style:UITableViewStylePlain];
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    self.setTableView.backgroundColor = [UIColor clearColor];
    self.setTableView.rowHeight = 60;
    self.setTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.setTableView.scrollEnabled = NO;
    [self.view addSubview:self.setTableView];
}
#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"mycell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.titleArr[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:22];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark tableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self isNight];
    } else if (indexPath.row == 1) {
        [self.setTableView reloadData];
        [self clearCache];
    } else if (indexPath.row == 2) {
        TLST_saveListVC *saveListVC = [[TLST_saveListVC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:saveListVC];
        [saveListVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:nav animated:YES completion:nil];
    }
}
// 夜间模式
- (void)isNight
{
    [TLSSingleton shareSingleton].isNight = ![TLSSingleton shareSingleton].isNight;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:[TLSSingleton shareSingleton].isNight] forKey:@"isNight"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isNight" object:nil];
}
// 清除缓存文件
- (void)clearCache
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    // 拿到JSON数据缓存文件夹路径
    self.cashPath = [sandBox[0] stringByAppendingPathComponent:@"travelWorld"];
    NSString *message = [NSString stringWithFormat:@"您确认清除%.1fM缓存",[self folderSizeAtPath:_cashPath]];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action){
            
            // 清除JSON数据缓存文件夹
            NSFileManager *manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:_cashPath error:nil];
            
            // SDImageCache 自带图片缓存清理操作
            [[SDImageCache sharedImageCache] cleanDisk];
            [self.setTableView reloadData];
        }];
        
        [alert addAction:cancelAction];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertView show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // 清除JSON数据缓存文件夹
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager removeItemAtPath:_cashPath error:nil];
        
        // SDImageCache 自带图片缓存清理操作
        [[SDImageCache sharedImageCache] cleanDisk];
        [self.setTableView reloadData];
    }
}
// 遍历文件夹获得文件夹大小,返回多少M
- (float)folderSizeAtPath:(NSString *)folderPath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    
    // JSON数据缓文件存大小
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];
    }
    // SDWebImage 框架自身计算图片缓存大小方法
    folderSize += [[SDImageCache sharedImageCache] getSize];
    
    // 返回值是字节 B K M
    return folderSize/(1024.0*1024.0);//Byte(字节)-->M
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
